//
//  LeftSonViewController.m
//  XLZChat
//
//  Created by dllo on 16/10/21.
//  Copyright © 2016年 徐昊. All rights reserved.
//  点头像网名进入的VC  LST

#import "LeftSonViewController.h"
#import "LeftTableViewCell.h"
#import "Masonry.h"

#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
@interface LeftSonViewController ()<UITableViewDelegate, UITableViewDataSource>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 背景View*/
@property (nonatomic, strong) UIView *backGroundView;
/** 背景图片*/
@property (nonatomic, strong) UIImageView *backimageView;
/** bar背景图片*/
@property (nonatomic, strong) UIImageView *barImageView;
/** 导航label */
@property (nonatomic, strong) UILabel *labelTitle;
/** 头像 */
@property (nonatomic, strong) UIImageView *imagePhoto;
/** 网名 */
@property (nonatomic, strong) UILabel *labelName;
/** 年龄 */
@property (nonatomic, strong) UILabel *labelAge;
/** 个性签名 */
@property (nonatomic, strong) UILabel *labelSignature;
@end

@implementation LeftSonViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatTableView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftBtn:)];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    //    self.navigationController.navigationBar.hidden = YES;
    _barImageView.alpha = 0;
    //    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0];
}
// 创建tableView
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEIGHT + 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[LeftTableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.tableHeaderView = [self creatHeaderView];
    self.barImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, 64)];
    _barImageView.image = [UIImage imageNamed:@"tts-navbar"];
    [self.navigationController.navigationBar addSubview:_barImageView];
}
// 创建头视图
- (UIView *)creatHeaderView {
    self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 2)];
    self.backimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, _backGroundView.bounds.size.height / 2)];
    _backimageView.image = [UIImage imageNamed:@"背景3"];
    [self.backGroundView addSubview:_backimageView];
    self.imagePhoto = [[UIImageView alloc] init];
    _imagePhoto.backgroundColor = [UIColor redColor];
    _imagePhoto.layer.cornerRadius = 50;
    [_backGroundView addSubview:_imagePhoto];
    [_imagePhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backGroundView.mas_centerX);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(100);
        make.centerY.mas_equalTo(_backimageView.mas_bottom);
    }];
    self.labelName = [[UILabel alloc] init];
    _labelName.numberOfLines = 0;
    _labelName.textAlignment = 1;
    _labelName.textColor = [UIColor blackColor];
    _labelName.backgroundColor = [UIColor yellowColor];
    [_backGroundView addSubview:_labelName];
    [_labelName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backGroundView.mas_centerX);
        make.width.mas_equalTo(_backGroundView.frame.size.width / 2);
        make.height.mas_equalTo(35);
        make.top.mas_equalTo(_imagePhoto.mas_bottom).offset(10);
    }];
    self.labelAge = [[UILabel alloc] init];
    _labelAge.backgroundColor = [UIColor blueColor];
    _labelAge.textColor = [UIColor blackColor];
    _labelAge.numberOfLines = 0;
    _labelAge.textAlignment = 1;
    _labelAge.font = [UIFont systemFontOfSize:15];
    [_backGroundView addSubview:_labelAge];
    [_labelAge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backGroundView.mas_centerX);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(_labelName.mas_bottom).offset(10);
    }];
    self.labelSignature = [[UILabel alloc] init];
    _labelSignature.backgroundColor = [UIColor redColor];
    _labelSignature.textColor = [UIColor blackColor];
    _labelSignature.numberOfLines = 0;
    _labelSignature.textAlignment = 1;
    _labelSignature.font = [UIFont systemFontOfSize:15];
    [_backGroundView addSubview:_labelSignature];
    [_labelSignature mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_labelAge.mas_bottom).offset(5);
        make.left.mas_equalTo(_backGroundView.mas_left).offset(30);
        make.right.mas_equalTo(_backGroundView.mas_right).offset(-30);
        make.height.mas_greaterThanOrEqualTo(50);
    }];
    
    return _backGroundView;
}
#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        return cell;
}
#pragma mark - ScrollView上推渐变, 下拉放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 上推
    CGFloat offset_Y = scrollView.contentOffset.y;
    CGFloat alpha = (offset_Y + 64) / (_backGroundView.bounds.size.height / 2);
    self.barImageView.alpha = alpha;
    if (alpha < 1) {
        self.navigationItem.title = @"";
    } else {
        //        self.navigationController.navigationBar.hidden = NO;
        self.navigationItem.title = @"资料";
        //self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:alpha];
    }
    // 下拉
    if (offset_Y < -64) {
        CGFloat addHeight = -(offset_Y + 64);
        CGFloat scale = (_backGroundView.bounds.size.height / 2 + addHeight) /(_backGroundView.bounds.size.height / 2);
        self.backimageView.frame = CGRectMake(-(WIDTH * scale - WIDTH) / 2.0f, -addHeight, WIDTH * scale, _backGroundView.bounds.size.height / 2 + addHeight);
    }
}
#pragma mark - 按钮点击方法
- (void)clickLeftBtn:(UIBarButtonItem *)leftBtn {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_barImageView removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
